use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub repository_url: String,
    pub models_dir: PathBuf,
    pub default_runner: String,
    pub enable_telemetry: bool,
    pub log_level: String,
    pub color_output: bool,
}

impl Default for Config {
    fn default() -> Self {
        let home_dir = dirs::home_dir().expect("Failed to get home directory");
        Self {
            repository_url: "https://models.yam.org/v1/".into(),
            models_dir: home_dir.join(".yam").join("models"),
            default_runner: "cpu".into(),
            enable_telemetry: true,
            log_level: "info".into(),
            color_output: true,
        }
    }
}

impl Config {
    pub fn load() -> Result<Self, config::ConfigError> {
        let builder = config::Config::builder()
            .add_source(config::File::with_name("config.yaml").required(false))
            .add_source(config::Environment::with_prefix("YAM"))
            .set_defaults(Config::default())?;

        builder.build()?.try_deserialize()
    }
}
